#!/usr/bin/python2.5
"""
    Googbus - Engine with DBUS interface for accessing Google APIs
    
    Copyright (C) 2008  Matti Raty

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/.
"""

import gtk
import logging
import googdefs
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gobject import MainLoop 
import googcalendar

class GoogbusCalendarService(dbus.service.Object):
    """
    Google calendar dbus service.
    
    Supported methods for calendar are:
        - Login to Google service
        - Listing all user's calendars
        - Listing calendar entries
        - Listing entries from today
        
        
    This class is just receiver for dbus methods and forwards all 
    requests to actual calendar class GoogCalendar (googcalendar.py).
    See GoogCalendar for full comments on methods.
    """
    LOG_PREFIX = "GoogbusCalendarService: "
    
    def __init__(self, bus_name):        
        dbus.service.Object.__init__(self, None, 
                                     googdefs.GOOGBUS_DBUS_CALENDAR_OBJECT, bus_name)
        
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)        
    def Login(self):
        """
        Login to Google service.
        """
        logging.debug(GoogbusCalendarService.LOG_PREFIX + "Login")
        self.googcalendar = googcalendar.GoogCalendar()
        return self.googcalendar.Login()
        
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)
    def ListAllCalendars(self):
        """
        Lists all user's calendars
        
        Returns tuple containing calendar names
        """
        logging.debug(GoogbusCalendarService.LOG_PREFIX + "ListAllCalendars")
        return self.googcalendar.ListAllCalendars()
    
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)
    def ListEntries(self):
        """
        List calendar entries.
        
        Returns tuple containing tuples of calendar entry information
        """
        logging.debug(GoogbusCalendarService.LOG_PREFIX + "ListEntries")
        return self.googcalendar.ListEntries()
    
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)
    def ListEntriesFromToday(self):
        """
        Lists entries from today and onwards
        
        Returns tuple containing tuples of calendar entry information and
        count of events in tuple. We need count because dbus can't handle
        empty tuples.
        """
        logging.debug(GoogbusCalendarService.LOG_PREFIX + "ListEntriesFromToday")
        events, count = self.googcalendar.ListEntriesFromNow()
        if count > 0:
            return events, count
        else:
            return (0), 0
    
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)
    def InsertNewEvent(self, title, content, location,
                       start_time, end_time):
        """
        Inserts new event to calendar.
        
        start_time and end_time are in ISO 8601 format.
        
        Returns 0 on success, otherwise error code
        """ 
        logging.debug(GoogbusCalendarService.LOG_PREFIX + "InsertNewEvent")
        return self.googcalendar.InsertNewEvent(title, content, location, start_time, end_time)
        
    
class GoogbusTestService(dbus.service.Object):
    """
    Test dbus service.
    This can be used to test dbus interface.
    """
    
    LOG_PREFIX = "GoogbusTestService: "
    
    def __init__(self, bus_name):        
        dbus.service.Object.__init__(self, None, 
                                     googdefs.GOOGBUS_DBUS_TEST_OBJECT, bus_name)
    
    @dbus.service.method(googdefs.GOOGBUS_DBUS_TEST_INTERFACE)
    def GetTestTuple(self):
        logging.debug(GoogbusTestService.LOG_PREFIX + "GetTestTuple")
        return ("Hello", "from", "test object") 


def __main__():
    """"
    We need main loop for dbus
    """
    mainloop_wrapper = DBusGMainLoop()
    dbus.set_default_main_loop(mainloop_wrapper)
    mainloop = MainLoop()

    """
    Setup dbus and all Googbus service objects
    """
    session_bus = dbus.SessionBus()
    name = dbus.service.BusName(googdefs.GOOGBUS_DBUS_SERVICE, bus=session_bus)
    calendar_object = GoogbusCalendarService(name)
    test_obj = GoogbusTestService(name)

    mainloop.run()

    
"""
Lets start!
"""
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/tmp/googbus.log',
                    filemode='a')

logging.debug('Starting Googbus receiver..')

__main__()

logging.debug('Googbus receiver died!')